package cn.sinata.wanglin.neighbor.activity;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.hyphenate.easeui.EaseConstant;
import com.xilada.xldutils.activitys.TitleActivity;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.view.ExpandableHeightListView;

import java.util.ArrayList;

import butterknife.BindView;
import cn.sinata.wanglin.MyApplication;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.adapter.GroupAdapter;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.db.dao.GroupDao;
import cn.sinata.wanglin.db.dao.GroupUserDao;
import cn.sinata.wanglin.db.dao.UserDao;
import cn.sinata.wanglin.model.Group;
import cn.sinata.wanglin.model.GroupList;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

public class GroupListActivity extends TitleActivity {

    @BindView(R.id.mSwipeRefreshLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @BindView(R.id.ehlv_create)
    ExpandableHeightListView ehlv_create;
    @BindView(R.id.ehlv_join)
    ExpandableHeightListView ehlv_join;
    @BindView(R.id.ehlv_housing)
    ExpandableHeightListView ehlv_housing;
    @BindView(R.id.tv_empty_create_group)
    TextView tv_empty_create_group;
    @BindView(R.id.tv_empty_join_group)
    TextView tv_empty_join_group;
    @BindView(R.id.tv_empty_housing_group)
    TextView tv_empty_housing_group;
    private GroupAdapter createAdapter ;
    private GroupAdapter joinAdapter ;
    private GroupAdapter housingAdapter ;
    private ArrayList<Group> createGroups = new ArrayList<>();
    private ArrayList<Group> joinGroups = new ArrayList<>();
    private ArrayList<Group> housingGroups = new ArrayList<>();
    private String userId;

    @Override
    protected int setContentId() {
        return R.layout.activity_my_groups_layout;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("聊天群");

        addRightButton("创建", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityUtil.create(mContext)
                        .go(CreateGroupChatStepOneActivity.class)
                        .startForResult(33);
            }
        });
        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);

        ehlv_create.setExpanded(true);
        ehlv_join.setExpanded(true);
        ehlv_housing.setExpanded(true);
        createAdapter = new GroupAdapter(this,createGroups);
        ehlv_create.setAdapter(createAdapter);
        joinAdapter = new GroupAdapter(this,joinGroups);
        ehlv_join.setAdapter(joinAdapter);
        housingAdapter = new GroupAdapter(this,housingGroups);
        ehlv_housing.setAdapter(housingAdapter);

        ehlv_create.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Group group = createGroups.get(i);
                String account = SharedPreferencesUtils.getString(Constant.SP_IM_ACCOUNT);
                String headUrl = SharedPreferencesUtils.getString(Constant.SP_HEADURL);
                //存储当前用户在群组的昵称和头像
                GroupUserDao.getInstance(mContext).saveGroupUser(group.getGroupAccount(),account,group.getOtherNickName(),headUrl);
                //存储群组头像昵称。
                UserDao.getInstance(mContext).saveUser(group.getGroupAccount(),group.getGroupName(),group.getGroupImg());
                //存储群信息
                GroupDao.getInstance(mContext).saveGroup(userId,group);

                ActivityUtil.create(mContext)
                        .go(ChatActivity.class)
                        .put(EaseConstant.EXTRA_CHAT_TYPE,EaseConstant.CHATTYPE_GROUP)
                        .put(EaseConstant.EXTRA_USER_ID,group.getGroupAccount())
                        .startForResult(44);
//                ActivityUtil.create(mContext)
//                        .go(GroupInfoActivity.class)
//                        .put("id",createGroups.get(i).getId())
//                        .put("name",createGroups.get(i).getGroupName())
//                        .startForResult(44);
            }
        });

        ehlv_join.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                ActivityUtil.create(mContext)
//                        .go(GroupInfoActivity.class)
//                        .put("id",joinGroups.get(i).getId())
//                        .put("name",joinGroups.get(i).getGroupName())
//                        .startForResult(44);

                Group group = joinGroups.get(i);
                String account = SharedPreferencesUtils.getString(Constant.SP_IM_ACCOUNT);
                String headUrl = SharedPreferencesUtils.getString(Constant.SP_HEADURL);
                //存储当前用户在群组的昵称和头像
                GroupUserDao.getInstance(mContext).saveGroupUser(group.getGroupAccount(),account,group.getGroupNickName(),headUrl);
                //存储群组头像昵称。
                UserDao.getInstance(mContext).saveUser(group.getGroupAccount(),group.getGroupName(),group.getGroupImg());
                //存储群信息
                GroupDao.getInstance(mContext).saveGroup(userId,group);

                ActivityUtil.create(mContext)
                        .go(ChatActivity.class)
                        .put(EaseConstant.EXTRA_CHAT_TYPE,EaseConstant.CHATTYPE_GROUP)
                        .put(EaseConstant.EXTRA_USER_ID,group.getGroupAccount())
                        .startForResult(44);
            }
        });

        ehlv_housing.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ActivityUtil.create(mContext)
                        .go(GroupInfoActivity.class)
                        .put("id",housingGroups.get(i).getId())
                        .put("name",housingGroups.get(i).getGroupName())
                        .startForResult(44);
            }
        });

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });

        showDialog();
        getData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 33 || requestCode == 44) {
                showDialog();
                getData();
            }
        } else if (resultCode == Constant.ResultCode_QUITE) {
            showDialog();
            getData();
        }
    }

    void getData(){
        HttpManager.myGroup(userId, MyApplication.houseid)
                .subscribe(new ResultDataSubscriber<GroupList>(this) {
                    @Override
                    public void onSuccess(String msg,GroupList groupList) {
                        mSwipeRefreshLayout.setRefreshing(false);
                        createGroups.clear();
                        joinGroups.clear();
                        housingGroups.clear();

                        ArrayList<Group> create = groupList.getCreateGroups();
                        if (create.size() == 0) {
                            tv_empty_create_group.setVisibility(View.VISIBLE);
                            ehlv_create.setVisibility(View.GONE);
                        } else {
                            tv_empty_create_group.setVisibility(View.GONE);
                            ehlv_create.setVisibility(View.VISIBLE);
                        }
                        createGroups.addAll(create);
                        createAdapter.notifyDataSetChanged();
                        ArrayList<Group> join = groupList.getJoinGroups();
                        if (join.size() == 0) {
                            tv_empty_join_group.setVisibility(View.VISIBLE);
                            ehlv_join.setVisibility(View.GONE);
                        } else {
                            tv_empty_join_group.setVisibility(View.GONE);
                            ehlv_join.setVisibility(View.VISIBLE);
                        }
                        joinGroups.addAll(join);
                        joinAdapter.notifyDataSetChanged();
                        ArrayList<Group> housing = groupList.getHousingGroups();
                        if (housing.size() == 0) {
                            tv_empty_housing_group.setVisibility(View.VISIBLE);
                            ehlv_housing.setVisibility(View.GONE);
                        } else {
                            tv_empty_housing_group.setVisibility(View.GONE);
                            ehlv_housing.setVisibility(View.VISIBLE);
                        }
                        housingGroups.addAll(housing);
                        housingAdapter.notifyDataSetChanged();

                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                });
    }
}
